import 'dart:async';

import 'package:flutter/cupertino.dart';

class DanmuItemView extends StatefulWidget {
  const DanmuItemView({super.key});

  @override
  State<DanmuItemView> createState() => _DanmuItemViewState();
}

class _DanmuItemViewState extends State<DanmuItemView>
    with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> _animation;
  late Timer timer;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (BuildContext context, Widget? child) {
        debugPrint(_animation.value.toString());
        debugPrint(MediaQuery.of(context).size.width.toString());
        return Transform.translate(
          offset:
              Offset(_animation.value * MediaQuery.of(context).size.width, 0),
          child: Text('弹幕'),
        );
      },
    );
  }

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(vsync: this, duration: const Duration(seconds: 10));
    controller.addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed) {
        controller.dispose();
      }
    });

    _animation = Tween(begin: 1.0, end: -1.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: Curves.linear,
      ),
    );
    timer = Timer(const Duration(seconds: 3), () {
      debugPrint('定时器关闭没有');
      if (controller != null) {
      controller.forward();
       }
    });
  }

  @override
  void dispose() {
    if(timer.isActive){
      timer.cancel();
    }
    if (controller != null) {
      if(controller.isAnimating){
        controller.stop();
        controller.dispose();
      }

    }
    super.dispose();
  }
}
